//
//  VoteHelper.m
//  meiyinxiang
//
//  Created by 媒印象 on 06/12/2017.
//  Copyright © 2017 hbjApple. All rights reserved.
//

#import "VoteHelper.h"

@implementation VoteHelper
+ (VoteHelper *)shared{
    static VoteHelper *helper = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        helper = [[VoteHelper alloc] init];
    });
    return helper;
}
- (NSString *)verifyVoteEnableWithPhoneNum:(NSString *)phoneNum
                                      name:(NSString *)name
                                       age:(NSString *)age
                                    imgurl:(NSString *)imageUrl{
    if (phoneNum.length < 11) {
        return @"请填写正确位数的手机号";
    }
    if (name.length < 2) {
        return @"请填写正确的姓名";
    }
    if (age.integerValue < 18 || age.integerValue > 50) {
        return @"您的年龄不符合我们活动要求";
    }
    if (imageUrl.length == 0) {
        return @"请上传您的照片";
    }
    return nil;
}
- (void)upLoadImg:(UIImage *)img
       completion:(uploadImgBlock)completion{
    [[Http sharedInstance] uploadImgDataWithUrl:MYY_upload_URL withParams:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        NSData *imgscreenData = UIImageJPEGRepresentation(img, 0.2);
        [formData appendPartWithFileData:imgscreenData name:@"file" fileName:@"pic.jpg" mimeType:@"image/jpg"];
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        CGFloat progress = uploadProgress.completedUnitCount/uploadProgress.totalUnitCount;
        NSLog(@"上传进度 %f",progress);
        [SVProgressHUD showProgress:progress status:@"上传中..."];
        if (progress == 1) {
            [SVProgressHUD dismiss];
        }
    } completion:^(id data, NSError *error) {
        
        if (data) {
            int  nRet = [[data objectForKey:@"status"] intValue];
            if (nRet == 1) {
                NSString *imagurl = data[@"data"];
                NSLog(@"imgurl is %@",imagurl);
                completion(YES,imagurl);
            }
        }else{
            completion(NO,nil);
        }
        
        
    }];
}
/**
 报名接口
 
 @param phoneNum 手机号码
 @param name 姓名
 @param age 年龄
 @param imgurl 上传的照片url
 @param completion 返回结果
 */
- (void)applyActivityWithPhoneNum:(NSString *)phoneNum
                             name:(NSString *)name
                              age:(NSString *)age
                           imgurl:(NSString *)imgurl
                       completion:(uploadImgBlock)completion{
    NSDictionary *para = @{
                           @"name":name,
                           @"age":age,
                           @"tel":phoneNum,
                           @"image":imgurl
                           };
    [[MYXHttpManager shared] applyActivityWithPara:para Result:^(BOOL sucess, NSString *msg) {
        if (sucess) {
            completion(YES,@"报名成功");
        }else{
            completion(NO,msg);
            ShowSVProgressErrorMessage(msg);

          //  [SVProgressHUD showErrorWithStatus:msg];
        }
    } errorResult:^(NSError *enginerError) {
        completion(NO,@"网络出错");
    }];
}
- (CGFloat)getTopLayoutConsWithdiffrentDivice {
    if (iphone4S) {
        return 230;
    }else if (iphone5S){
        return 275;
    }else if (iphone6){
        return 320;
    }else if (iphone6P){
        return 358;
    }else{
        return 358;
    }
}
@end
